import { Effect, Reducer } from 'umi';
import myApi from '@/servers/Api';
import {
  deleteData,
  getData,
  postData,
  putData,
  postFile,
} from '@/servers/Http';
import { message } from 'antd';

export interface FaqModelState {
  faqList: object;
  queryForm: object;
  pagination: object;
  categoriesList: object;
  fileList: object;
}

export interface FaqModelType {
  namespace: 'faqModel';
  state: FaqModelState;
  effects: {
    getFaqList: Effect;
    onDeleted: Effect;
    tableChange: Effect;
    getCategoriesList: Effect;
    create: Effect;
    edit: Effect;
    uploadFile: Effect;
  };
  reducers: {
    setFaqList: Reducer;
    setPagination: Reducer;
    setQueryForm: Reducer;
    setCategoriesList: Reducer;
    setFileList: Reducer;
  };
  subscriptions: {};
}

const FaqModel: FaqModelType = {
  namespace: 'faqModel',
  state: {
    faqList: [],
    queryForm: {},
    pagination: {
      total: 0,
      pageSize: 10,
      current: 1,
    },
    categoriesList: [],
    fileList: [], // 文件列表
  },
  effects: {
    *getFaqList({ payload }, { put }) {
      let res = yield getData(myApi.getFaqList, { ...payload });
      if (res) {
        const { headers } = res;
        let params = {
          faqList: res.data,
        };
        let pagination = {
          total: Number(headers['x-pagination-total-count']),
          pageSize: Number(headers['x-pagination-per-page']),
          current: Number(headers['x-pagination-current-page']),
        };
        yield put({ type: 'setFaqList', payload: params });
        yield put({ type: 'setPagination', payload: { pagination } });
        yield put({ type: 'setQueryForm', payload });
      }
    },
    *onDeleted({ payload }, { put }) {
      let res = yield deleteData(`${myApi.getFaqList}/${payload.id}`);
      console.log(res);
      if (res && (res.status == 204 || res.status == 200)) {
        message.success('删除成功');
        yield put({ type: 'getFaqList' });
      }
    },
    *tableChange({ payload }, { put }) {
      yield put({ type: 'setPagination', payload });
      yield put({
        type: 'getFaqList',
        payload: {
          'per-page': payload.pagination.pageSize,
          page: payload.pagination.current,
        },
      });
    },
    *getCategoriesList(_, { put }) {
      let res = yield getData(`${myApi.getCategories}?alias=user_type`);
      if (res) {
        let params = {
          categoriesList: res.data,
        };
        yield put({ type: 'setCategoriesList', payload: params });
      }
    },
    *create({ payload }, { put }) {
      let res = yield postData(myApi.getFaqList, { ...payload });
      if (res) {
        message.success('创建成功');
        yield put({ type: 'getFaqList' });
      }
    },
    *edit({ payload }, { put }) {
      let res = yield putData(`${myApi.getFaqList}/${payload.id}`, {
        ...payload,
      });
      if (res) {
        message.success('编辑成功');
        yield put({ type: 'getFaqList' });
      }
    },
    *uploadFile({ payload }, { put }) {
      let res = yield postFile(myApi.upload, { file: payload, type: 'file' });
      if (res) {
        const { status } = res;
        if (status >= 200 && status < 300) {
          let fileList = [];
          fileList.push(payload);
          message.success('上传成功');
          yield put({ type: 'setFileList', payload: { fileList } });
        }
      }
    },
  },
  reducers: {
    setFaqList(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setPagination(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setQueryForm(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setCategoriesList(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setFileList(state, action) {
      console.log(action.payload);
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {
    // setup({dispatch, history}) {
    //   return history.listen(({pathname}) => {
    //     if (pathname === '/') {
    //       dispatch({
    //         type: 'query',
    //       });
    //     }
    //   });
    // },
  },
};
export default FaqModel;
